/*
 *  CEEditBox.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/controls/CEEditBox.h"

#include "cocos2dx_extentions/labels/CELabelBMFont.h"
#include "cocos2dx_extentions/utility/CECocos2dUtility.h"
#include "cocos2dx_extentions/localization/CELocale.h"

using namespace cocos2d;

namespace cocosext {

/////////////////////////////////////////////////////
//
/** CEEditBox 
* 
*/
/////////////////////////////////////////////////////

CEEditBox* CEEditBox::node(const char* font,
						   float fontScale,
						   ccColor3B textColor,
						   cocos2d::CCSprite* normalLeft,
						   cocos2d::CCSprite* normalCenter,
						   cocos2d::CCSprite* normalRight,
						   cocos2d::CCSprite* pressedLeft,
						   cocos2d::CCSprite* pressedCenter,
						   cocos2d::CCSprite* pressedRight,
						   cocos2d::CCSprite* disabledLeft,
						   cocos2d::CCSprite* disabledCenter,
						   cocos2d::CCSprite* disabledRight)
{
	CEEditBox *pRet = new CEEditBox(font,
									fontScale,
									textColor,
									normalLeft,
									normalCenter,
									normalRight,
									pressedLeft,
									pressedCenter,
									pressedRight,
									disabledLeft,
									disabledCenter,
									disabledRight);
	CCAssert(pRet != NULL, NULL);
	if (pRet->init())
	{
		pRet->autorelease();
        return pRet;
	}
	else
	{
		CC_SAFE_DELETE(pRet)
		return NULL;
	}
}

bool CEEditBox::init()
{
    if (!CCNode::init() )
        return false;

	CCSize contentSize = _normalLeft->getContentSize();
	float leftShift = contentSize.width;
	contentSize.width += _normalCenter->getContentSize().width;
	contentSize.width += _normalRight->getContentSize().width;

	_normalLeft->setAnchorPoint(ccp(0.f, 0.5f));
	_normalCenter->setAnchorPoint(ccp(0.f, 0.5f));
	_normalRight->setAnchorPoint(ccp(1.f, 0.5f));
	_pressedLeft->setAnchorPoint(ccp(0.f, 0.5f));
	_pressedCenter->setAnchorPoint(ccp(0.f, 0.5f));
	_pressedRight->setAnchorPoint(ccp(1.f, 0.5f));
	_disabledLeft->setAnchorPoint(ccp(0.f, 0.5f));
	_disabledCenter->setAnchorPoint(ccp(0.f, 0.5f));
	_disabledRight->setAnchorPoint(ccp(1.f, 0.5f));

	_normalLeft->setPosition(ccp(0.f, contentSize.height/2.f));
	_normalCenter->setPosition(ccp(leftShift, contentSize.height/2.f));
	_normalRight->setPosition(ccp(contentSize.width, contentSize.height/2.f));
	_pressedLeft->setPosition(ccp(0.f, contentSize.height/2.f));
	_pressedCenter->setPosition(ccp(leftShift, contentSize.height/2.f));
	_pressedRight->setPosition(ccp(contentSize.width, contentSize.height/2.f));
	_disabledLeft->setPosition(ccp(0.f, contentSize.height/2.f));
	_disabledCenter->setPosition(ccp(leftShift, contentSize.height/2.f));
	_disabledRight->setPosition(ccp(contentSize.width, contentSize.height/2.f));

	_normalLeft->setIsVisible(true);
	_normalCenter->setIsVisible(true);
	_normalRight->setIsVisible(true);
	_pressedLeft->setIsVisible(false);
	_pressedCenter->setIsVisible(false);
	_pressedRight->setIsVisible(false);
	_disabledLeft->setIsVisible(false);
	_disabledCenter->setIsVisible(false);
	_disabledRight->setIsVisible(false);

	this->addChild(_normalLeft, 0);
	this->addChild(_normalCenter, 0);
	this->addChild(_normalRight, 0);
	this->addChild(_pressedLeft, 0);
	this->addChild(_pressedCenter, 0);
	this->addChild(_pressedRight, 0);
	this->addChild(_disabledLeft, 0);
	this->addChild(_disabledCenter, 0);
	this->addChild(_disabledRight, 0);

	//Frame
	_frame = CENodeScissored<CCNode>::node();
	_frame->setAnchorPoint(CCPointZero);
	_frame->setContentSize(_normalCenter->getContentSize());
	_frame->setPosition(ccp(leftShift, 0));
	this->addChild(_frame, 1);

	//Label
	_scrollableArea = CELabelBMFont::labelWithString(L"Empty string", CELOCALE->languageFilePath(_font.c_str()));
	_scrollableArea->setColor(_textColor);
	_scrollableArea->setAnchorPoint(ccp(0.f, 0.5f));
	_scrollableArea->setScale(_fontScale);
	_scrollableArea->setPosition(ccp(0.f, _frame->getContentSize().height/2.f));
	_frame->addChild(_scrollableArea, 0);

	//Set content size
	this->setContentSize(contentSize);

	return true;
}

/////////////////////////////////////////////////////
// Public interface

void CEEditBox::setText(const wchar_t* text)
{
	_scrollableArea->setString(text);
}

/////////////////////////////////////////////////////
//Cocos2d interface
void CEEditBox::setContentSize(const cocos2d::CCSize& contentSize)
{
	cocos2d::CCSize newContentSize = contentSize;
	if (newContentSize.height != _normalLeft->getContentSize().height)
		newContentSize.height = _normalLeft->getContentSize().height;

	float widthLeftRight = _normalLeft->getContentSize().width + _normalRight->getContentSize().width;
	bool noCenter = newContentSize.width <= widthLeftRight;
	if (noCenter)
	{
		newContentSize.width = widthLeftRight;
		_centerScaleX = 0.f;
	}
	else
	{
		_centerScaleX = (contentSize.width - widthLeftRight)/_normalCenter->getContentSize().width;
		_normalCenter->setScaleX(_centerScaleX);
	}
	
	switch(_state)
	{
	case eSEEditBoxStateNormal: _normalCenter->setIsVisible(!noCenter); break;
	case eSEEditBoxStatePressed: _pressedCenter->setIsVisible(!noCenter); break;
	case eSEEditBoxStateDisabled: _disabledCenter->setIsVisible(!noCenter); break;
	}

	_normalRight->setPosition(ccp(newContentSize.width, contentSize.height/2.f));
	_pressedRight->setPosition(ccp(newContentSize.width, contentSize.height/2.f));
	_disabledRight->setPosition(ccp(newContentSize.width, contentSize.height/2.f));

	_frame->setContentSize(CCSize(_normalCenter->getContentSize().width*_centerScaleX, _normalCenter->getContentSize().height));

	CCNode::setContentSize(newContentSize);
}

}; // end namespace cocosext

